package day_2022_9_to_12.Day;

import java.util.Stack;

/**
 * @author haomin
 * @date 2022/09/16 22:04
 **/
public class Day14_4_Num739_DailyTemperatures {
    class Solution {
        public int[] dailyTemperatures(int[] temperatures) {
            int n = temperatures.length;
            if(n == 1) return new int[]{0};
            int[] ans = new int[n];
            int l = 0, r = 1;
            while(r < n){
                while(r < n && temperatures[l] >= temperatures[r]) r++;
                if(r == n) ans[l] = 0;
                else ans[l] = r-l;
                l++;
                r = l+1;
            }
            ans[n-1] = 0;
            return ans;
        }
    }
    class Solution2 {
        public int[] dailyTemperatures(int[] temperatures) {
            Stack<Integer> stack = new Stack<>();
            int n = temperatures.length;
            int[] ans = new int[n];
            for(int i = 0; i < n; ++i){
                while(!stack.isEmpty() && temperatures[i] > temperatures[stack.peek()]){
                    int index = stack.pop();
                    ans[index] = i - index;
                }
                stack.push(i);
            }
            return ans;
        }
    }
}